package com.feng.common.exception;

import com.feng.common.web.WebCommonResponse;
import com.feng.common.web.WebCommonResponseBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.UnexpectedTypeException;

/**
 * @author Lee
 */
@ControllerAdvice
public class WebExceptionHandle {

    private final  static Logger LOGGER = LoggerFactory.getLogger(WebExceptionHandle.class);

    @ResponseBody
    @ExceptionHandler({UnexpectedTypeException.class})
    public WebCommonResponse<Void> hibernateValidationHandler(UnexpectedTypeException e){
        String desc = "参数错误：" + e.getMessage();
        LOGGER.info(desc);
        return new WebCommonResponseBuilder<Void>().fail(ErrorCode.CheckError.toString(), desc).build();
    }

    @ResponseBody
    @ExceptionHandler({ValidationException.class})
    public WebCommonResponse<Void> customerExceptionHandler(ValidationException e){
        String desc = "参数错误：" + e.getMessage();
        LOGGER.info(desc);
        return new WebCommonResponseBuilder<Void>().fail(ErrorCode.SystemError.toString(), desc).build();
    }

}
